<template>
  <el-container>
    <el-header>
      <h3>教学记录</h3>
    </el-header>
    <el-main>
      <el-form :inline="true" ref="form" :model="form">
        <el-form-item label="日期筛选:" prop="date">
          <el-date-picker type="date" v-model="form.date"></el-date-picker>
        </el-form-item>
        <el-form-item label="时间段选择:" prop="time_id">
          <el-select v-model="form.time_id">
            <el-option
              v-for="item in timeList"
              :value="item.id"
              :key="item.id"
              :label="item.time_limit"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教学科目:" prop="subject">
          <el-select v-model="form.subject">
            <el-option
              v-for="item in subjectList"
              :value="item.id"
              :key="item.id"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教学状态:" prop="status">
          <el-select v-model="form.status">
            <el-option
              v-for="item in statusList"
              :value="item.id"
              :key="item.id"
              :label="item.label"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click.prevent="search">查询</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click.prevent="reset('form')">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        border
        :data="tableData"
        v-loading="loading"
        :cell-style="changeStyle"
      >
        <el-table-column label="图像">
          <template slot-scope="scope">
            <el-avatar :src="scope.row.avatar"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="name"></el-table-column>
        <el-table-column label="手机号" prop="mobile"></el-table-column>
        <el-table-column label="日期" prop="date"></el-table-column>
        <el-table-column label="时间段" prop="time_limit"></el-table-column>
        <el-table-column label="科目" prop="subject"></el-table-column>
        <el-table-column label="状态" prop="status_name"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link v-if="scope.row.status == 1" @click="cancelTeach(scope.row.id)">取消</el-link>
            <el-link v-else disabled>无法操作</el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination
        background
        layout="prev, pager, next, total"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        @current-change="changePage"
      ></el-pagination>
    </el-footer>
  </el-container>
</template>

<script>
  import {getPracticeTime, getTeachList, cancelTeachReach} from '@/api/coach'
    export default {
        name: "teach",
        inject: ['reload'],
        data () {
            return {
                form: {
                  date: '',
                  time_id: 0,
                  subject: 0,
                  status: 0
                },
                timeList: [
                    {id: 0, time_limit: '全部'}
                ],
                subjectList: [
                    {id: 0, label: '全部'},
                    {id: 1, label: '科目二'},
                    {id: 2, label: '科目三'}
                ],
                statusList: [
                    {id: 0, label: '全部'},
                    {id: 1, label: '进行中'},
                    {id: 2, label: '已完成'},
                    {id: 3, label: '已取消'},
                    {id: 4, label: '未完成'}
                ],
                coach_id: 0,
                page: 1,
                total: 0,
                pageSize: 10,
                tableData: [],
                loading: true,
            };
        },
        methods: {
          search() {
              var params = {
                  coach_id: this.coach_id,
                  time_id: this.form.time_id,
                  subject: this.form.subject,
                  status:  this.form.status,
              };
              if (this.form.date.length != 0) {
                  params.date = this.form.date;
              }
              this.loading = true;
              getTeachList(params).then(response => {
                  this.tableData = response.data.list;
                  this.page = Number(response.data.paginate.current);
                  this.pageSize = Number(response.data.paginate.pageSize);
                  this.total = Number(response.data.paginate.total);
                  this.loading = false;
              }).catch( err => {
                  this.loading = false;
              })
          },
          reset(ref) {
              this.$refs[ref].resetFields();
          },
          changePage(currentPage) {
              var params = {
                  coach_id: this.coach_id,
                  time_id: this.form.time_id,
                  subject: this.form.subject,
                  status:  this.form.status,
                  page: currentPage,
                  pageSize: this.pageSize
              };
              if (this.form.date.length != 0) {
                  params.date = this.form.date;
              }
              this.loading = true;
              getTeachList(params).then(response => {
                  this.tableData = response.data.list;
                  this.page = Number(response.data.paginate.current);
                  this.pageSize = Number(response.data.paginate.pageSize);
                  this.total = Number(response.data.paginate.total);
                  this.loading = false;
              }).catch( err => {
                  this.loading = false;
              })
          },
          cancelTeach(teach_id){
              cancelTeachReach({teach_id: teach_id}).then(response => {
                this.$message.success(response.msg);
                this.reload();
              }).catch( err => {

              })
          },
          changeStyle (row, column, rowIndex, columnIndex) {
              //这里主要是修改状态那一列的值 颜色
              if (row.column.label == '状态') {
                  if (row.row.status_name == '进行中') {
                      return 'color: blue';
                  } else if (row.row.status_name == '已取消') {
                      return 'color: red';
                  }  else if (row.row.status_name == '未完成') {
                      return 'color: yellow';
                  } else if (row.row.status_name == '已完成') {
                      return 'color: green';
                  }
              }
          }
        },
        mounted() {
            this.coach_id = this.$route.query.coach_id;
            getPracticeTime({coach_id: this.coach_id}).then(response => {
                response.data.forEach(item => {
                    this.timeList.push(item);
                });
                getTeachList({coach_id: this.coach_id}).then(response => {
                  this.tableData = response.data.list;
                  this.page = response.data.paginate.current;
                  this.pageSize = response.data.paginate.pageSize;
                  this.total = response.data.paginate.total;
                  this.loading = false;
                })
            }).catch()
        }
    }
</script>

<style scoped>
  .el-container {
    width: 99%;
    max-width: 100%;
    overflow: hidden;
    border: 1px solid #F2F6FC;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
    margin: 10px auto;
  }
  .el-header{
    text-align: center;
  }
  .el-header h3 {
    font-size: 20px;
    height: 20px;
    line-height: 20px;
  }

  .el-footer{
    text-align: center;
  }
</style>
